﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Config;
using Framework.Library.Base.DataAccess;

namespace Framework.Portal.Layouts.Admin.Components.BGTT.Courses
{
    public partial class CategoriesManager : BaseControl
    {
        private readonly string MENUNAME = string.Empty;

        public CategoriesManager()
        {
        }

        public CategoriesManager(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblStatusUpdate.Text = string.Empty;
            if (!Page.IsPostBack)
            {
                lblCommandName.Text = MENUNAME;
                drpContentType.DataSource = StaticData.CategoriesType;
                drpContentType.DataBind();
                LoadZones();
            }
        }

        protected void lstZones_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDefault();
            if (lstZones.SelectedValue == "0")
                return;
            LoadInfo(ConvertUtility.ToInt32(lstZones.SelectedValue));
        }

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            try
            {
                ManagerFactory.DAOEntity.Insert(GetInfo());
                lblStatusUpdate.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            catch (Exception ex)
            {
                lblStatusUpdate.Text = ex.Message;
            }
            LoadZones();
            GetDefault();
        }

        protected void cmdEmpty_Click(object sender, EventArgs e)
        {
            GetDefault();
        }

        protected void cmdDel_Click(object sender, EventArgs e)
        {
            try
            {
                ManagerFactory.DAOEntity.Delete(new BGTTCategories { ID = ConvertUtility.ToInt32(lblID.Text) });
                lblStatusUpdate.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            catch (Exception ex)
            {
                lblStatusUpdate.Text = ex.Message;
            }
            LoadZones();
            GetDefault();
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            BGTTCategories info = GetInfo();           
            try
            {
                info.ID = Convert.ToInt32(lblID.Text);
                ManagerFactory.DAOEntity.Update(info);
                lblStatusUpdate.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            catch (Exception ex)
            {
                lblStatusUpdate.Text = ex.Message;
            }
            LoadZones();
            GetDefault();
        }

        protected void drpContentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadZones();
        }

        private void GetDefault()
        {
            lblID.Text = string.Empty;
            txtName.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtImage.Text = string.Empty;
        }

        private BGTTCategories GetInfo()
        {
            var info = new BGTTCategories();
            info.NAME = txtName.Text;
            info.DESCRIPTION = txtDescription.Text;
            info.AVATAR = txtImage.Text;
            info.TYPE = Convert.ToInt32(drpContentType.SelectedValue);
            return info;
        }

        protected void LoadZones()
        {
            List<BGTTCategories> dtCommands = ManagerFactory.DAOEntity.GetList(new BGTTCategories() { TYPE = Convert.ToInt32(drpContentType.SelectedValue) });
            lstZones.Nodes.Clear();
            if (dtCommands != null)
            {
                foreach (BGTTCategories row in dtCommands)
                {
                    var item = new TreeNode(row.NAME, row.ID.ToString());
                    lstZones.Nodes.Add(item);
                }
            }
        }

        private void LoadInfo(int zoneID)
        {
            BGTTCategories info = ManagerFactory.DAOEntity.GetInfo(new BGTTCategories() { ID = zoneID });
            if (info == null)
                return;
            lblID.Text = info.ID.ToString();
            txtName.Text = info.NAME;
            txtDescription.Text = info.DESCRIPTION;
            txtImage.Text = info.AVATAR;
        }
    }
}